package com.data.modules.smzh.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.data.common.api.vo.Result;
import com.data.common.aspect.annotation.AutoLog;
import com.data.common.system.base.controller.BootController;
import com.data.common.system.query.QueryGenerator;
import com.data.common.util.DeviceClientUtil;
import com.data.common.util.KamliClientUtil;
import com.data.modules.smzh.entity.Devicebuild;
import com.data.modules.smzh.entity.Deviceuser;
import com.data.modules.smzh.entity.Facedevice;
import com.data.modules.smzh.service.IDevicebuildService;
import com.data.modules.smzh.service.IDeviceuserService;
import com.data.modules.smzh.service.IFacedeviceService;
import com.data.modules.smzh.vo.SmDeviceVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;

 /**
 * @Description: facedevice
 * @Author: data-boot
 * @Date:   2022-11-29
 * @Version: V1.0
 */
@Api(tags="facedevice")
@RestController
@RequestMapping("/smzh/facedevice")
@Slf4j
public class FacedeviceController extends BootController<Facedevice, IFacedeviceService> {

	@Autowired
	private IFacedeviceService facedeviceService;
	@Autowired
	private IDevicebuildService dbuildService;

	@Autowired
	private IDeviceuserService deviceuserService;

	/**
	 * 分页列表查询
	 *
	 * @param facedevice
	 * @param pageNo
	 * @param pageSize
	 * @param req
	 * @return
	 */
	@AutoLog(value = "facedevice-分页列表查询")
	@ApiOperation(value="facedevice-分页列表查询", notes="facedevice-分页列表查询")
	@GetMapping(value = "/list")
	public Result<?> queryPageList(Facedevice facedevice,
								   @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
								   @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
								   HttpServletRequest req) {
		QueryWrapper<Facedevice> queryWrapper = QueryGenerator.initQueryWrapper(facedevice, req.getParameterMap());
		Page<Facedevice> page = new Page<Facedevice>(pageNo, pageSize);
		IPage<Facedevice> pageList = facedeviceService.page(page, queryWrapper);
		return Result.OK(pageList);
	}

	/**
	 *   添加
	 *
	 * @param facedevice
	 * @return
	 */
	@AutoLog(value = "facedevice-添加")
	@ApiOperation(value="facedevice-添加", notes="facedevice-添加")
	@PostMapping(value = "/add")
	public Result<?> add(@RequestBody Facedevice facedevice) {
		facedeviceService.save(facedevice);
		return Result.OK("添加成功！");
	}


	 @ApiOperation(value="绑定设备", notes="绑定设备")
	 @PostMapping(value = "/bindDevice")
	 public Result<?> bindDevice(@RequestBody SmDeviceVo smDeviceVo) {

		 String pwd = "PvUdG1";
		 //Facedevice facedevice
	/*	 if (ctx.request.body.type === 2) {
			 ctx.request.body.serialNo = _appid.app_id + ctx.request.body.uniqueNo + ctx.request.body.serialNo;
		 }
                const device = await ctx.service.deviceManage.findDeviceBySerialNo(ctx.request.body.serialNo);
*/

		 int i = facedeviceService.lambdaQuery().eq(Facedevice::getSerialNo,smDeviceVo.getSerialNo()).list().size();

		 if(i>0){
			 return Result.error("设备编号已存在！");
		 }

		 Boolean flag = facedeviceService.addBindDevice(smDeviceVo);
		 if(flag){
		 	 // 通知
			 if(ObjectUtil.equals(2,smDeviceVo.getType())){
				 pwd = "540039";
			 }

			 if(!KamliClientUtil.doPost(smDeviceVo.getSerialNo(),pwd)){
				 return Result.error("通知kamilis 失败！");
			 };

			 return Result.OK("添加成功！");
		 }else{
			 // facedeviceService.save(facedevice);
			 return Result.OK("添加失败！");
		 }
	 }


	 @ApiOperation(value="更新设备", notes="更新设备")
	 @PostMapping(value = "/updateDevice")
	 public Result<?> updateDevice(@RequestBody SmDeviceVo smDeviceVo) {

		 String pwd = "PvUdG1";
		 //Facedevice facedevice
	/*	 if (ctx.request.body.type === 2) {
			 ctx.request.body.serialNo = _appid.app_id + ctx.request.body.uniqueNo + ctx.request.body.serialNo;
		 }
                const device = await ctx.service.deviceManage.findDeviceBySerialNo(ctx.request.body.serialNo);
*/

		/* int i = facedeviceService.lambdaQuery().eq(Facedevice::getSerialNo,smDeviceVo.getSerialNo()).list().size();

		 if(i>0){
			 return Result.error("设备编号已存在！");
		 }*/

		 facedeviceService.updateBindDeviceInfo(smDeviceVo);

		 return Result.OK("操作成功！");
	 }

	 @ApiOperation(value="重启设备", notes="重启设备")
	 @PostMapping(value = "/reboot")
	 public Result<?> reboot(@RequestBody SmDeviceVo smDeviceVo) {

		 if(ObjectUtil.isNotEmpty(smDeviceVo.getSerialNo())){
			 DeviceClientUtil.doPostJson(smDeviceVo.getSerialNo());
		 }

		 return Result.OK("重启成功！");
	 }
	 @ApiOperation(value="删除设备", notes="删除设备")
	 @PostMapping(value = "/unbindDevice")
	 public Result<?> unbindDevice(@RequestBody SmDeviceVo smDeviceVo) {

		 LambdaQueryWrapper<Facedevice> wrapper = new LambdaQueryWrapper();
		 wrapper.eq(Facedevice::getSerialNo,smDeviceVo.getSerialNo());
		 wrapper.eq(Facedevice::getAppId,smDeviceVo.getAppId());
		 facedeviceService.remove(wrapper);

		 LambdaQueryWrapper<Devicebuild> wrapper1 = new LambdaQueryWrapper();
		 wrapper1.eq(Devicebuild::getSerialNo,smDeviceVo.getSerialNo());
		 dbuildService.remove(wrapper1);

		 LambdaQueryWrapper<Deviceuser> wrapper2 = new LambdaQueryWrapper();
		 wrapper2.eq(Deviceuser::getSerialNo,smDeviceVo.getSerialNo());

		 deviceuserService.remove(wrapper2);

		 return Result.OK("重启成功！");
	 }



	 /**
	 *  编辑
	 *
	 * @param facedevice
	 * @return
	 */
	@AutoLog(value = "facedevice-编辑")
	@ApiOperation(value="facedevice-编辑", notes="facedevice-编辑")
	@PutMapping(value = "/edit")
	public Result<?> edit(@RequestBody Facedevice facedevice) {
		facedeviceService.updateById(facedevice);
		return Result.OK("编辑成功!");
	}

	/**
	 *   通过id删除
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "facedevice-通过id删除")
	@ApiOperation(value="facedevice-通过id删除", notes="facedevice-通过id删除")
	@DeleteMapping(value = "/delete")
	public Result<?> delete(@RequestParam(name="id",required=true) String id) {
		facedeviceService.removeById(id);
		return Result.OK("删除成功!");
	}

	/**
	 *  批量删除
	 *
	 * @param ids
	 * @return
	 */
	@AutoLog(value = "facedevice-批量删除")
	@ApiOperation(value="facedevice-批量删除", notes="facedevice-批量删除")
	@DeleteMapping(value = "/deleteBatch")
	public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
		this.facedeviceService.removeByIds(Arrays.asList(ids.split(",")));
		return Result.OK("批量删除成功!");
	}

	/**
	 * 通过id查询
	 *
	 * @param id
	 * @return
	 */
	@AutoLog(value = "facedevice-通过id查询")
	@ApiOperation(value="facedevice-通过id查询", notes="facedevice-通过id查询")
	@GetMapping(value = "/queryById")
	public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
		Facedevice facedevice = facedeviceService.getById(id);
		if(facedevice==null) {
			return Result.error("未找到对应数据");
		}
		return Result.OK(facedevice);
	}

    /**
    * 导出excel
    *
    * @param request
    * @param facedevice
    */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, Facedevice facedevice) {
        return super.exportXls(request, facedevice, Facedevice.class, "facedevice");
    }

    /**
      * 通过excel导入数据
    *
    * @param request
    * @param response
    * @return
    */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, Facedevice.class);
    }

}
